#!/usr/bin/env python
# encoding: utf-8
"""
FLVMovie.py

Created by Manabu Terada on 2009-09-24.
Copyright (c) 2009 CMScom. All rights reserved.
"""
from AccessControl import ClassSecurityInfo
from ComputedAttribute import ComputedAttribute
from zope import interface

from Products.Archetypes.atapi import *
from Products.CMFCore.utils import getToolByName

from Products.ATContentTypes.content.base import ATCTContent
from Products.ATContentTypes.content.schemata import finalizeATCTSchema
from Products.ATContentTypes.configuration import zconf
from Products.CMFCore.permissions import View, ModifyPortalContent

from c2.site.icos09 import icos09MessageFactory as _
from c2.site.icos09.content.interfaces import IFLVMovie
from c2.site.icos09.config import *
from c2.site.icos09.swfHeaderData import analyseContent

schema = Schema((
    StringField('youtube_url', # URL
                required=True,
                validators = ('isURL',),
                widget=StringWidget(label='youtube_url',
                                description='',
                                label_msgid = "label_youtube_url",
                                description_msgid = 'help_youtube_url',
                                i18n_domain = "icos09",
                                size=40
                                ),
                ),

    ),)
flvmovie_schema = getattr(ATCTContent, 'schema', Schema(())).copy() + schema.copy()
# flvmovie_schema['relatedItems'].widget.visible['edit'] = 'invisible'
finalizeATCTSchema(flvmovie_schema)

class FLVMovie(ATCTContent):
    """
    For YouTube
    """
    security = ClassSecurityInfo()
    interface.implements(IFLVMovie)
    schema = flvmovie_schema
    
    meta_type = "FLVMovie"
    _at_rename_after_creation = True
    
    security.declareProtected(View, 'get_video_url')
    def get_video_url(self):
        url = self.youtube_url
        try:
            video_id = url.split('v=')[1].split('&')[0]
        except:
            video_id = ''
        video_url = 'http://www.youtube.com/v/' + video_id
        return video_url

registerType(FLVMovie, PROJECTNAME)
